package com.formula.propertyshop.view.phone;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.formula.propertyshop.R;
import com.formula.propertyshop.model.Contact;
import com.formula.propertyshop.model.CourseRegisterRequest;
import com.formula.propertyshop.model.POSTResponse;
import com.formula.propertyshop.service.IWireFrameService;
import com.formula.propertyshop.service.RestTemplateService;
import com.formula.propertyshop.service.ServiceConfigure;
import com.formula.propertyshop.service.WireFrameServiceFactory;
import com.formula.propertyshop.utils.Const;
import com.formula.propertyshop.utils.WFErrorCode;
import com.formula.propertyshop.utils.WFUtils;
import com.formula.propertyshop.view.common.BaseActivity;
import com.squareup.picasso.Picasso;

public class ContactActivity extends BaseActivity{

	private String action;
	private TextView txtEmail;
	private TextView txtName;
	private TextView txtPhone;
	private TextView txtContent;
	private Button btnSend;
	@Override
	protected void initUI() {
		setContentView(R.layout.contact_main);
		txtPhone = (TextView) findViewById(R.id.txtPhone);
		txtName = (TextView) findViewById(R.id.txtName);
		txtEmail = (TextView) findViewById(R.id.txtEmail);
		txtContent = (TextView) findViewById(R.id.txtContent);
		btnSend = (Button) findViewById(R.id.btnSend);	
		action = getIntent().getStringExtra(Const.ACTIVITY_ACTION);
		TextView title = (TextView) findViewById(R.id.txtTitle);
		if(Const.ACTION_REGISTER_COURSE.equalsIgnoreCase(action))
			title.setText(getString(R.string.cource));
		else
			title.setText(getString(R.string.contactus));
		loadData();
		btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!validateData())
					return;
				showProgressDlg();
				saveData();
				
				if(Const.ACTION_REGISTER_COURSE.equalsIgnoreCase(action)){
					handleRegisterCourse();
				}else{
					handleSendContact();
				}
			}
		});
	}
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if(msg.arg1 == WFErrorCode.NETWORK_ERROR.getValue()){
				showNetworkErrorDlg();
				return;
			}
			else if(msg.obj!=null){
				POSTResponse reponse = (POSTResponse) msg.obj;
				if(Const.ACTION_REGISTER_COURSE.equalsIgnoreCase(action)){
					if(reponse.isResult()){
						Toast.makeText(ContactActivity.this, R.string.register_course_sucess, Toast.LENGTH_SHORT).show();
						finish();
					}
					else{
						Toast.makeText(ContactActivity.this, R.string.register_course_unsucess, Toast.LENGTH_SHORT).show();
					}
				}
				else{
					if(reponse.isResult()){
						Toast.makeText(ContactActivity.this, R.string.send_contact_sucess, Toast.LENGTH_SHORT).show();
						finish();
					}
					else{
						Toast.makeText(ContactActivity.this, R.string.send_contact_unsucess, Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
	};
	private void loadData(){
		String imgUrl = RestTemplateService.getInstance().getServiceConfig().getContactInfo();
		Picasso.with(this).load(imgUrl).fit().error(R.drawable.noimage).placeholder(R.drawable.noimage).into((ImageView)findViewById(R.id.imgView));
		String name = WFUtils.getValue(this, Const.NAME_KEY);
		String email = WFUtils.getValue(this,Const.EMAIL_KEY);
		if(email.isEmpty())
			email = WFUtils.getPrimaryEmail(this);
		String phone = WFUtils.getValue(this, Const.PHONE_KEY);
		if(phone.isEmpty())
			phone = WFUtils.getPhoneNumber(this);
		txtName.setText(name);
		txtEmail.setText(email);
		txtPhone.setText(phone);
	}
	private void saveData(){
		String name = txtName.getText().toString();
		String email = txtEmail.getText().toString();
		String phone = txtPhone.getText().toString();
		WFUtils.saveValue(this, Const.NAME_KEY, name);
		WFUtils.saveValue(this, Const.PHONE_KEY, phone);
		WFUtils.saveValue(this, Const.EMAIL_KEY, email);
	}
	private boolean validateData(){
		Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence_button);
		if(txtName.getText().toString().isEmpty()){
			Toast.makeText(this, R.string.msg_field_not_empty, Toast.LENGTH_SHORT).show();
			txtName.startAnimation(animFadein);
			return false;
		}
		if(txtPhone.getText().toString().isEmpty()){
			Toast.makeText(this, R.string.msg_field_not_empty, Toast.LENGTH_SHORT).show();
			txtPhone.startAnimation(animFadein);
			return false;
		}
		if(txtEmail.getText().toString().isEmpty()){
			Toast.makeText(this, R.string.msg_field_not_empty, Toast.LENGTH_SHORT).show();
			txtEmail.startAnimation(animFadein);
			return false;
		}
		if(txtContent.getText().toString().isEmpty()){
			Toast.makeText(this, R.string.msg_field_not_empty, Toast.LENGTH_SHORT).show();
			txtContent.startAnimation(animFadein);
			return false;
		}
		return true;
	}
	private void handleSendContact(){
		final Contact contact = new Contact();
		contact.content = txtContent.getText().toString();
		contact.email = txtEmail.getText().toString();
		contact.name = txtName.getText().toString();
		contact.phone = txtPhone.getText().toString();
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message msg= handler.obtainMessage();
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();						
				try {
					POSTResponse response = service.sendContact(contact);
					msg.obj = response;
				} catch (Exception e) {
					e.printStackTrace();
					msg.arg1 = WFErrorCode.NETWORK_ERROR.getValue();
				}
				handler.sendMessage(msg);
			}
		});
		thread.start();
	}
	private void handleRegisterCourse(){
		final CourseRegisterRequest course = new CourseRegisterRequest();
		course.content = txtContent.getText().toString();
		course.email = txtEmail.getText().toString();
		course.name = txtName.getText().toString();
		course.phone = txtPhone.getText().toString();
		course.course_id = getIntent().getIntExtra(Const.COURSEID, 0)+"";
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message msg= handler.obtainMessage();
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();						
				try {
					POSTResponse response = service.registerCourse(course);
					msg.obj = response;
				} catch (Exception e) {
					e.printStackTrace();
					msg.arg1 = WFErrorCode.NETWORK_ERROR.getValue();
				}
				handler.sendMessage(msg);
			}
		});
		thread.start();
	}
}
